<?php

namespace App\Http\Controllers;

use App\Models\Campus;
use App\Models\Packages;
use App\Models\Staffs;
use Illuminate\Http\Request;

class StaffController extends Controller
{
    public function getInfo(Request $request){
        $staff = Staffs::where("id" , $request->object_id)->first();
        $data = [
            'id' => $staff->id,
            'firstname' => $staff->first_name,
            'lastname' => $staff->last_name,
            'phone_number' => $staff->phone,
            'email' => $staff->email,
            'online' => $staff->online,
        ];
        if ($staff->position == 0){
            $data["affiliated_campus"] = Campus::where("id" , $staff->campus_id)->first()->name;
            $data['remaining_capacity'] = 5 - $staff->package_number;
            $data['total_picked'] = $staff->total_picked;
            $data['total_delivered'] = $staff->total_delivered;
        }else{
            $data['plate_number'] = $staff->plate;
            $data['current_campus'] = Campus::where("id" , $staff->campus_id)->first()->name;
            $data['remaining_capacity'] = 15 - $staff->package_number;
            $data['total_unloaded'] = $staff->total_unloaded;
        }
        return $this->returnJson($data);
    }

    public function changeOnline(Request $request){
        $staff = Staffs::where('id' , $request->object_id)->first();
        $staff->online = $staff->online == 1 ? '0' : '1';
        $staff->save();
        return $this->returnJson([
            'id' => $staff->id,
            'online' => $staff->online,
        ]);
    }
}
